import {
	mapState
} from 'vuex'
import {
	getMycollection,
	getJxzDetail,
	getHasNft,
	getHasNfts,
	openBoxX,
	medalNft,
	nftPopUps,
	hasNftIsBrooch,
	storageNftList
} from '../../api/collection.js'
export default {
	data() {
		return {
			list: [],
			isShowOpen: false,
			openData: {},
			blindBoxType: false,
			storage: false,
			collectionNum: 0, //藏品总数量
			collectionStorage: [], //藏品收纳
			counts: 0, //藏品列表总共数量
			arr: [] //最终数组
			// onShowFuns:onShowFun
		}
	},
	computed: {
		...mapState({
			user: state => state.userInfo,
			token: state => state.userToken,
			giftType: state => state.giftType
		})
	},
	created() {
		if (this.token) {
			// this.getBenCollection()
		}
	},
	onPullDownRefresh() {
		if (this.token) {
			this.closeBox()
		}
		setTimeout(() => {
			uni.stopPullDownRefresh()
		}, 1000)
	},
	onShow() {
		// console.log(111,  && this.giftType)
		if (this.token) {
			this.closeBox()
			// this.giftType = false
		}
	},
	methods: {
		async openBlind() {
			try {
				const res = await openBoxX({
					boxId: this.openData.id
				})
				this.openData.name = res.data.nft_title
				this.openData.token_id = res.data.token_id
				this.openData.nft_img_url = res.data.nft_img_url
				this.blindBoxType = false
				this.isShowOpen = true
			} catch (e) {
				uni.showToast({
					title: e.message,
					icon: 'none',
					duration: 2000
				})
			}
		},
		handle(item) {
			if (item.boxType == 1) {
				this.goJxz()
			} else {
				this.goBenCao(item)
			}
		},
		goJxz() {
			uni.navigateTo({
				url: '/pages/collection/jxzDetail'
			})
		},
		async goBenCao(item) {
			//先去判断是谁调用的接口数据模式   1.李  2.向
			if (item.apiTypeState == 1) {
				if (item.type) {
					uni.navigateTo({
						url: `/pages/collection/detail?id=${item.id}&apiTypeState=${item.apiTypeState}`,
						fail(e) {
							console.log(e)
						}
					})
				} else {
					if (this.user.isAttested) {
						this.isShowOpen = true
						this.openData = item
					} else {
						uni.showModal({
							title: '提示',
							content: '您还未实名认证，不能开启盲盒哦。是否去认证？',
							confirmText: '去认证',
							confirmColor: '#000000',
							cancelColor: '#999999',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/user/realName'
									})
								}
							}
						})
					}
				}
			} else {
				if (item.collectionType == 1) { //判断是否是普通藏品
					let url;
					if (item.type) { //藏品
						url = `id=${item.id}`
					} else { //盲盒
						if (item.status) { //已开启
							url = `id=${item.id}`
						} else { //没开启
							this.openData = item
							this.blindBoxType = true
						}
					}
					if (item.type || item.type.status) {
						uni.navigateTo({
							url: `/pages/collection/superblockChain?${url}&apiTypeState=${item.apiTypeState}&collectionType=${item.collectionType}`,
							fail(e) {
								console.log(e)
							}
						})
					}
				} else if (item.collectionType == 2) { //胸针藏品
					if (!item.locked) { //未开启
						let url = '';
						const token = this.$store.state.collectionToken
						url = `${item.nft_url.split('#')[0]}?token=${token}#${item.nft_url.split('#')[1]}`
						uni.navigateTo({
							url: `/pages/collection/otherPage?url=${url}`
						})
					} else if (!item.storageType) { //已开启
						uni.navigateTo({
							url: `/pages/collection/superblockChain?id=${item.id}&apiTypeState=${item.apiTypeState}&collectionType=${item.collectionType}`,
							fail(e) {
								console.log(e)
							}
						})
					} else if (item.storageType) {
						uni.navigateTo({
							url: item.goToUrl,
							fail(e) {
								console.log(e)
							}
						})
					}
				}
			}
		},
		gotoPages() {
			this.list = []
			this.getBenCollection()
			this.getJxzCollection()
			this.blindBoxType = false
			this.isShowOpen = false
			uni.navigateTo({
				url: `/pages/collection/superblockChain?id=${this.openData.id}&apiTypeState=${this.openData.apiTypeState}&collectionType=${this.openData.collectionType}`,
				fail(e) {
					console.log(e)
				}
			})
		},
		async getBenCollection() {
			uni.showLoading({
				title: '加载中...'
			})
			try {
				const storageNftLists = await storageNftList()
				const nftPopUpss = await nftPopUps() //空投拉跨勋章
				// const resHasNft = await getHasNfts()	//藏品列表
				// const medalNfts = await medalNft() //胸针藏品
				// const hasNftIsBroochs = await hasNftIsBrooch() //胸针藏品列表
				if (JSON.stringify(nftPopUpss.data) != "{}") {
					let cont = nftPopUpss.data
					let datas = {
						...cont,
						name: cont.nft_title,
						collectionType: 1, //藏品类型 1为普通藏品
						apiTypeState: 2
					}
					this.openData = datas
					this.isShowOpen = true
				}
				console.log(1111, this.arr)
				let dataList = storageNftLists.data.data
				this.counts = dataList.count
				this.collectionStorage = dataList.storage
				if (dataList.is_baidu) {
					const res = await getMycollection() //百度链藏品列表
					if (res.data.data.length) {
						res.data.data.forEach(item => {
							const type = item.status === 1 && item.type === 0 ? 0 : 1
							this.arr.push({
								image: item.image,
								cover: type ? item.image : require(
									'@/static/img/collection-close-cover.png'),
								id: item.id,
								type,
								title: type ? item.title : '本草纲目盲盒',
								realTitle: item.title,
								boxType: 2, // 1.姜小竹 2.本草纲目
								imgUrl: item.image,
								name: item.title,
								time: res.data.time,
								apiTypeState: 1
							})
						})
					}
				}
				if (dataList.list.length) {
					dataList.list.forEach((item, index) => {
						if (item.type) { //藏品
							this.arr.push({
								...item,
								imgUrl: item.nft_img_url,
								name: item.nft_title,
								collectionType: 1, //藏品类型 1为普通藏品
								apiTypeState: 2,
								nftPopupType: 1
							})
						} else { //盲盒
							if (item.status) { //已开启
								this.arr.push({
									...item,
									imgUrl: item.nft_img_url,
									name: item.nft_title,
									collectionType: 1, //藏品类型 1为普通藏品
									apiTypeState: 2
								})
							} else { //没开启
								console.log(7777,item)
								this.arr.unshift({
									...item,
									imgUrl: item.blind_box_img_url,
									name: item.blind_box_name,
									collectionType: 1, //藏品类型 1为普通藏品
									apiTypeState: 2
								})
							}
						}
					})
				}
				// if (medalNfts.data.data) {
				// 	arr.unshift({
				// 		...medalNfts.data.data,
				// 		imgUrl: medalNfts.data.data.nft_img_url,
				// 		name: medalNfts.data.data.nft_name,
				// 		collectionType: 2, //藏品类型 2为胸针藏品
				// 		apiTypeState: 2,
				// 	})
				// }
				// //若胸针藏品数量大于三 进行收纳
				// if(resHasNft.data.storage){
				// 	arr.unshift({
				// 		imgBgkUrl: '/static/img/storageBck.png', //背景图
				// 		imgUrl: '/static/img/storageBrooch.png', //展示图
				// 		titleNameType: 1, //胸针收纳
				// 		name: '艾草胸针系列藏品',//收纳名称
				// 		goToUrl: '/pages/collection/storageBrooch',//跳转路径
				// 		storageType: true ,//是否收纳
				// 		collectionType: 2,
				// 		apiTypeState: 2,
				// 		locked: true
				// 	})
				// }
				// this.collectionNum = resHasNft.data.list.length + res.data.data.length + hasNftIsBroochs.data.length
				this.list = this.arr
				this.collectionStorage.forEach((item, index) => {
					this.list.unshift(this.collectionStorage[index])
				})
				// this.storage = resHasNft.data.storage
				this.getJxzCollection()
				uni.hideLoading()
				this.loading = false
			} catch (e) {
				console.log(e)
				uni.hideLoading()
				this.loading = false
			}
		},
		//获取百度链本草纲目藏品
		async getBaiduCollection() {

			return arr
		},
		async getJxzCollection() {
			try {
				const res = await getJxzDetail()
				if (res.data.data) {
					const data = {
						imgUrl: require('@/static/img/collection-jxz-cover.png'),
						id: 0,
						type: 1,
						name: '姜小竹',
						boxType: 1 // 1.姜小竹 2.本草纲目
					}
					this.list.push(data)
					console.log(11111, this.list)
				}
			} catch (e) {
				console.log(e)
				uni.hideLoading()
				this.loading = false
				// uni.showToast({
				// 	title: e.message || e.data,
				// 	icon: 'none'
				// })
			}
		},
		closeBox() {
			this.isShowOpen = false
			this.list = []
			this.arr = []
			this.collectionStorage = []
			this.getBenCollection()
			// this.getJxzCollection()
		}
	}
}
